package heima.lm.oschina.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;

import java.util.HashMap;
import java.util.Map;

import heima.lm.oschina.BaseApplication;
import heima.lm.oschina.R;
import heima.lm.oschina.base.BaseActivity;
import heima.lm.oschina.bean.Blog;
import heima.lm.oschina.bean.BlogDetail;
import heima.lm.oschina.bean.News;
import heima.lm.oschina.common.CommonPager;
import heima.lm.oschina.constant.Constants;
import heima.lm.oschina.utils.HttpUtils;
import heima.lm.oschina.utils.StringUtils;
import heima.lm.oschina.utils.UIHelper;

/**
 * Created by 金毛兄弟 on 2017/4/6.
 */

public class BlogDetailActivity extends BaseActivity {

    private CommonPager mCommonPager;
    private View mSuccessView;
    private String mId;
    private Blog mBlog;
    private WebView mWebView;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mId = getIntent().getStringExtra("blog_detail_id");
        Log.d("lm", "Blog Id:" + mId);
        mCommonPager = new CommonPager() {
            @Override
            protected void loadData() {
                BlogDetailActivity.this.loadData();
            }

            @Override
            protected void loadSuccessPager() {
                BlogDetailActivity.this.loadSuccessPager();
            }
        };
        setContentView(mCommonPager.mRootView);
        BaseApplication.mActivityList.add(this);
    }


    @Override
    protected void onStart() {
        super.onStart();
        mCommonPager.dynamic();
    }

    private void loadData() {
        HttpUtils<BlogDetail> httpUtils = new HttpUtils<>();
        Map<String, String> parmas = new HashMap<>();
        parmas.put("/", mId);
        httpUtils.getData(Constants.Http.BLOG_DETAIL, parmas, BlogDetail.class, new HttpUtils.OnHttpResponseListener<BlogDetail>() {
            @Override
            public void onSuccess(BlogDetail bean) {
                mBlog = bean.getBlog();
                mCommonPager.dealCommonSuccess(mBlog);
            }

            @Override
            public void onFailure(Exception e) {
                mCommonPager.dealCommonFailure();

            }
        });

    }

    private void loadSuccessPager() {
        mSuccessView = View.inflate(this, R.layout.acivity_detail, null);
        initView();
        mWebView.loadDataWithBaseURL("", getWebViewBody(mBlog), "text/html", "UTF-8", "");
        mCommonPager.changeView(mSuccessView);
    }

    private void initView() {
        Toolbar toolbar = (Toolbar) mSuccessView.findViewById(R.id.toolbar_detail);
        setSupportActionBar(toolbar);
        //博客详情
        toolbar.setTitle(R.string.actionbar_title_blog);
        //toobal的箭头
        toolbar.setNavigationIcon(R.drawable.actionbar_back_icon_normal);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BlogDetailActivity.this.finish();
            }
        });
        mWebView = (WebView) mSuccessView.findViewById(R.id.wv_detail);
        WebSettings settings = mWebView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setSupportZoom(true);
        settings.setBuiltInZoomControls(true);

    }

    public String getWebViewBody(Blog blog) {
        StringBuffer body = new StringBuffer();
        body.append(UIHelper.WEB_STYLE).append(UIHelper.WEB_LOAD_IMAGES);
        // 添加title
        body.append(String.format("<div class='title'>%s</div>", blog.getTitle()));
        // 添加作者和时间
        String time = StringUtils.friendly_time(blog.getPubDate());
        String author = String.format("<a class='author' href='http://my.oschina.net/u/%s'>%s</a>", blog.getAuthorId(), blog.getAuthor());
        body.append(String.format("<div class='authortime'>%s&nbsp;&nbsp;&nbsp;&nbsp;%s</div>", author, time));
        body.append(UIHelper.setHtmlCotentSupportImagePreview(blog.getBody()));
        // 更多关于***软件的信息
      /*  String softwareName = blog.get
        String softwareLink = blog.getSoftwareLink();
        if (!StringUtils.isEmpty(softwareName)
                && !StringUtils.isEmpty(softwareLink))
            body.append(String
                    .format("<div class='oschina_software' style='margin-top:8px;font-weight:bold'>更多关于:&nbsp;<a href='%s'>%s</a>&nbsp;的详细信息</div>",
                            softwareLink, softwareName));
*/
        /*// 相关新闻
        if (blog.getNotice() != null
                && blogDetail.getRelatives().size() > 0) {
            String strRelative = "";
            for (News.Relative relative : blogDetail.getRelatives()) {
                strRelative += String.format(
                        "<li><a href='%s' style='text-decoration:none'>%s</a></li>",
                        relative.url, relative.title);
            }
            body.append("<p/><div style=\"height:1px;width:100%;background:#DADADA;margin-bottom:10px;\"/>"
                    + String.format("<br/> <b>相关资讯</b><ul class='about'>%s</ul>",
                    strRelative));
        }*/
        body.append("<br/>");
        // 封尾
        body.append("</div></body>");
        return body.toString();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        //webView支持回退操作
        if (keyCode == KeyEvent.KEYCODE_BACK && mWebView.canGoBack()) {
            mWebView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


}
